import request from '@/utils/request';
import { requestWrapper, randomDate, randomInt, randomPick, paginate } from '@/utils/mock';

// 获取字典类型列表
export function getDictTypeList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/type/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const dictTypes = Array.from({ length: 20 }, (_, index) => ({
        dictId: index + 1,
        dictName: `字典${index + 1}`,
        dictType: `dict_type_${index + 1}`,
        status: randomPick(['0', '1']),
        remark: `字典${index + 1}的描述`,
        createTime: randomDate(-30)
      }));

      // 处理搜索条件
      let filteredDictTypes = [...dictTypes];

      if (params) {
        if (params.dictName) {
          filteredDictTypes = filteredDictTypes.filter(item => 
            item.dictName.includes(params.dictName)
          );
        }
        
        if (params.dictType) {
          filteredDictTypes = filteredDictTypes.filter(item => 
            item.dictType.includes(params.dictType)
          );
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredDictTypes = filteredDictTypes.filter(item => 
            item.status === params.status
          );
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredDictTypes.slice(startIndex, endIndex);

      return paginate(pageData, pageNum, pageSize, filteredDictTypes.length);
    }
  );
}

// 获取字典类型详情
export function getDictTypeDetail(dictId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/type/${dictId}`,
      method: 'get'
    }),
    // 模拟数据
    {
      dictId,
      dictName: `字典${dictId}`,
      dictType: `dict_type_${dictId}`,
      status: '1',
      remark: `字典${dictId}的描述`,
      createTime: randomDate(-30),
      updateTime: randomDate(-10)
    }
  );
}

// 新增字典类型
export function addDictType(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/type',
      method: 'post',
      data
    }),
    // 模拟数据
    {
      dictId: randomInt(100, 999)
    }
  );
}

// 更新字典类型
export function updateDictType(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/type',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 删除字典类型
export function deleteDictType(dictIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/type/${dictIds}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 获取字典数据列表
export function getDictDataList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/data/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      // 预定义一些样式类
      const listClasses = ['default', 'primary', 'success', 'info', 'warning', 'danger'];
      
      const dictData = Array.from({ length: 15 }, (_, index) => ({
        dictCode: index + 1,
        dictType: params?.dictType || 'test_dict_type',
        dictLabel: `选项${index + 1}`,
        dictValue: `${index + 1}`,
        dictSort: index + 1,
        listClass: randomPick(listClasses),
        cssClass: '',
        status: randomPick(['0', '1']),
        remark: `选项${index + 1}的描述`,
        createTime: randomDate(-30)
      }));

      // 处理搜索条件
      let filteredDictData = [...dictData];

      if (params) {
        if (params.dictType) {
          filteredDictData = filteredDictData.filter(item => 
            item.dictType === params.dictType
          );
        }
        
        if (params.dictLabel) {
          filteredDictData = filteredDictData.filter(item => 
            item.dictLabel.includes(params.dictLabel)
          );
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredDictData = filteredDictData.filter(item => 
            item.status === params.status
          );
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredDictData.slice(startIndex, endIndex);

      return paginate(pageData, pageNum, pageSize, filteredDictData.length);
    }
  );
}

// 获取字典数据详情
export function getDictDataDetail(dictCode) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/data/${dictCode}`,
      method: 'get'
    }),
    // 模拟数据
    {
      dictCode,
      dictType: 'test_dict_type',
      dictLabel: `选项${dictCode}`,
      dictValue: `${dictCode}`,
      dictSort: dictCode,
      listClass: 'default',
      cssClass: '',
      status: '1',
      remark: `选项${dictCode}的描述`,
      createTime: randomDate(-30),
      updateTime: randomDate(-10)
    }
  );
}

// 新增字典数据
export function addDictData(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/data',
      method: 'post',
      data
    }),
    // 模拟数据
    {
      dictCode: randomInt(100, 999)
    }
  );
}

// 更新字典数据
export function updateDictData(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/dict/data',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 删除字典数据
export function deleteDictData(dictCodes) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/data/${dictCodes}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 根据字典类型获取字典数据
export function getDictDataByType(dictType) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/dict/data/type/${dictType}`,
      method: 'get'
    }),
    // 模拟数据
    () => {
      // 预定义一些常用的字典类型
      const commonDictTypes = {
        sys_user_sex: [
          { dictCode: 1, dictLabel: '男', dictValue: '0', dictSort: 1, listClass: 'default', cssClass: '', status: '1' },
          { dictCode: 2, dictLabel: '女', dictValue: '1', dictSort: 2, listClass: 'default', cssClass: '', status: '1' },
          { dictCode: 3, dictLabel: '未知', dictValue: '2', dictSort: 3, listClass: 'default', cssClass: '', status: '1' }
        ],
        sys_normal_disable: [
          { dictCode: 4, dictLabel: '正常', dictValue: '1', dictSort: 1, listClass: 'success', cssClass: '', status: '1' },
          { dictCode: 5, dictLabel: '停用', dictValue: '0', dictSort: 2, listClass: 'danger', cssClass: '', status: '1' }
        ],
        sys_show_hide: [
          { dictCode: 6, dictLabel: '显示', dictValue: '1', dictSort: 1, listClass: 'success', cssClass: '', status: '1' },
          { dictCode: 7, dictLabel: '隐藏', dictValue: '0', dictSort: 2, listClass: 'warning', cssClass: '', status: '1' }
        ]
      };

      // 如果是预定义的字典类型，返回对应的字典数据
      if (commonDictTypes[dictType]) {
        return commonDictTypes[dictType];
      }
      
      // 对于未预定义的字典类型，生成随机数据
      return Array.from({ length: 5 }, (_, index) => ({
        dictCode: randomInt(1000, 9999),
        dictLabel: `${dictType}选项${index + 1}`,
        dictValue: `${index + 1}`,
        dictSort: index + 1,
        listClass: 'default',
        cssClass: '',
        status: '1',
        remark: `${dictType}选项${index + 1}的描述`
      }));
    }
  );
} 